package com.walleipt.ipt_tools.utils;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/** 
 * 本程序生成一个AES密钥，并且转换它为RAW字节，然后根据这个密钥重新创建一个AES密钥，这个新构建的密钥用于初始化一个加密解密的AES密码 
 */  
  
public class AESCodec {  
  
	private static final Logger LOG = LoggerFactory.getLogger(AESCodec.class);
	
	private static final String salt = "0123456789012345" ;
	
    public static String aes_encrypt(String password, String strKey) {  
        try {  
            byte[] keyBytes = Arrays.copyOf(strKey.getBytes("ASCII"), 16);  
  
            SecretKey key = new SecretKeySpec(keyBytes, "AES");  
            Cipher cipher = Cipher.getInstance("AES");  
            cipher.init(Cipher.ENCRYPT_MODE, key);  
  
            byte[] cleartext = password.getBytes("UTF-8");  
            byte[] ciphertextBytes = cipher.doFinal(cleartext);  
  
            return new String(Hex.encodeHex(ciphertextBytes)).toUpperCase();  
        } catch (UnsupportedEncodingException e) {  
        	LOG.info( e.getMessage());
        } catch (NoSuchAlgorithmException e) {  
        	LOG.info( e.getMessage());
        } catch (NoSuchPaddingException e) {  
        	LOG.info( e.getMessage());
        } catch (InvalidKeyException e) {  
        	LOG.info( e.getMessage());
        } catch (IllegalBlockSizeException e) {  
        	LOG.info( e.getMessage());
        } catch (BadPaddingException e) {  
        	LOG.info( e.getMessage());
        } 
        return "-1" ;
    }  
      
    public static String aes_decrypt(String encryptResult, String strKey) {  
        try {  
            byte[] keyBytes = Arrays.copyOf(strKey.getBytes("ASCII"), 16);  
  
            SecretKey key = new SecretKeySpec(keyBytes, "AES");  
            Cipher cipher = Cipher.getInstance("AES");  
            cipher.init(Cipher.DECRYPT_MODE, key);  
  
            byte[] cleartext = Hex.decodeHex(encryptResult.toCharArray());  
            byte[] ciphertextBytes = cipher.doFinal(cleartext);  
  
            return new String(ciphertextBytes, "UTF-8");  
        } catch (UnsupportedEncodingException e) {  
        	LOG.info( e.getMessage());
        	
        } catch (NoSuchAlgorithmException e) {  
        	LOG.info( e.getMessage());
        } catch (NoSuchPaddingException e) {  
        	LOG.info( e.getMessage());
        } catch (InvalidKeyException e) {  
        	LOG.info( e.getMessage());
        } catch (IllegalBlockSizeException e) {  
        	LOG.info( e.getMessage());
        } catch (BadPaddingException e) {  
        	LOG.info( e.getMessage());
        } catch (DecoderException e) {  
        	LOG.info( e.getMessage());
        }
        return "-1" ;
    }  
    
    
    public static String encode( String content ) {
    	return aes_encrypt( content, salt) ;
    }
    
    public static String decode( String enCode) {
    	return aes_decrypt( enCode, salt) ;
    }
      
    public static void main(String[] args) {  
//        String content = "password";  
//        String password = "0123456789012345";  
//        // 加密  
//        System.out.println("加密前：" + content);  
//        String encryptResult = aes_encrypt(content, password);  
//        System.out.println("加密后：" + encryptResult);  
//        // 解密  
//        String decryptResult = aes_decrypt(encryptResult, password);  
//        System.out.println("解密后：" + decryptResult);  
//  
    	System.err.println("----------------------- begin ----------------------\n");  
    	for (int i = 1032; i < 1060; i++) {
    		System.out.print("加密前：" + i);  
            String encryptResult = aes_encrypt(ConvertUtils.toString( i ), salt);  
            System.out.println("加密后：" + encryptResult);  
		}
    	System.err.println("\n----------------------- end ----------------------"); 
    	
    	
//    	  String content = "1002";  
//          // 加密  
//          System.out.println("加密前：" + content);  
//          String encryptResult = aes_encrypt(content, salt);  
//          System.out.println("加密后：" + encryptResult);  
//          // 解密  
//          String decryptResult = aes_decrypt(encryptResult, salt);  
//          System.out.println("解密后：" + decryptResult);  
    
    }  
  
}  
